# coding: UTF-8

import rest_core
import json


def check_result(response_json) :

    return_info = response_json['output']['result-info']

    print 'RESULT INFO = ' + return_info

    if return_info == 'TRUE' :
        print '===>>> Proccss Success.'
    else :
        print '===>>> Process Failure.'

    print '-------------------------------------------'


def execute_request(exec_op, input_data):

    if input_data :
        data = {}
        data['input'] = input_data
        payload = json.dumps(data)
    else :
        payload = None

    print payload

    return rest_core.action_post(exec_op, payload)


def init_topology_data(input_data) :
    '''
    input_data format:
    * resource-location - topo json file pwd
    '''

    print 'Init Topology Data: '

    exec_op = 'tno:init-topology-data'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def init_tunnel_info_data(input_data) :
    '''
    input_data format:
    * resource-location - tunnelInfo json file pwd
    '''

    print 'Init Tunnel Info: '

    exec_op = 'tno:init-tunnel-info-data'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def init_tunnel_stat_data(input_data) :
    '''
    input_data format:
    * resource-location - tunnelStat json file pwd
    '''

    print 'Init Tunnel Stat: '

    exec_op = 'tno:init-tunnel-stat-data'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def create_tunnel_info(input_data) :
    '''
    input_data format:
    * name - tunnelInfo name
    * priority - tunnel priority
    * src-ip - tunnel source ip-address
    * dst-ip - tunnel destination ip-address
    * bandwidth-demand - tunnel bandwidth demand
    * delay-demand - tunnel delay demand
    * hop-demand - tunnel hop demand
    * pass-nodes
    * pass-links
    * avoid-nodes
    * avoid-links
    * pass-sequences
    '''

    print 'Create Tunnel Info: '

    exec_op = 'tno:create-tunnel-info'
    response = execute_request(exec_op, input_data)

    response_json = response.json()
    tunnel_id = response_json['output']['tunnel-id']
    print 'Created Tunnel Info ID = %s.' % (tunnel_id)

    check_result(response.json())

    return tunnel_id


def renew_tunnel_info(input_data) :
    '''
    input_data format:
    * target-tunnel-info-id
    * updated-bandwidth-demand
    * updated-delay-demand
    * updated-hop-demand
    '''

    print 'Renew Tunnel Info: '

    exec_op = 'tno:renew-tunnel-info'
    response = execute_request(exec_op, input_data)

    tunnel_id = input_data['target-tunnel-info-id']
    print 'Updated Tunnel Info ID = %s.' % (tunnel_id)

    check_result(response.json())


def remove_tunnel_info(input_data) :
    '''
    input_data format:
    * target-tunnel-info-id
    '''
    print 'Remove Tunnel Info: '

    exec_op = 'tno:remove-tunnel-info'
    response = execute_request(exec_op, input_data)

    tunnel_id = input_data['target-tunnel-info-id']
    print 'Delete Tunnel Info ID = %s.' % (tunnel_id)

    check_result(response.json())


def dump_event_log():
    print 'Dump Event Log: '

    exec_op = 'tno:dump-event-log'
    response = execute_request(exec_op, None)

    check_result(response.json())


def import_current_network(input_data) :
    '''
    input_data format:
    * gold-flow
    * silver-flow
    * iron-flow
    * copper-flow
    '''

    print 'Import Current Network Traffic: '

    exec_op = 'tno:import-current-network'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def launch_specific_traffic_model(input_data) :
    '''
    input_data format:
    * traffic-model-number
    * uniform-range
    * burst-prob
    * burst-range
    * fluctuation-range
    * gold-range
    * silver-range
    * iron-range
    * copper-range
    * user-defined-resource
    '''

    print 'Launch Specific Traffic Model: '

    exec_op = 'tno:launch-specific-traffic-model'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def execute_fault_recovery(input_data) :
    '''
    input_data format:
    * fault-nodes (list)
    * fault-links (list)
    '''

    print 'Execute Fault Recovery: '

    exec_op = 'tno:execute-fault-recovery'
    response = execute_request(exec_op, input_data)

    check_result(response.json())


def launch_periodic_global_optimization() :
    print 'Launch Periodic Global Optimization: '

    exec_op = 'tno:launch-periodic-global-optimization'
    response = execute_request(exec_op, None)

    check_result(response.json())

def regulate_pce_config(input_data) :
    print 'Regulate PCE Configuration: '

    data = {}
    data['tpce-config'] = input_data
    cfg_set = 'tpce:tpce-config'
    response = rest_core.cfgdb_put(cfg_set, json.dumps(data))

    if response.status_code == 200 :
        print 'Update PCE Configuration Success.'
    else :
        print 'Update PCE Configuration Failure.'


def regulate_tno_config(input_data) :
    print 'Regulate TNO Configuration: '

    data = {}
    data['tno-config'] = input_data
    cfg_set = 'tno:tno-config'
    response = rest_core.cfgdb_put(cfg_set, json.dumps(data))

    if response.status_code == 200 :
        print 'Update TNO Configuration Success.'
    else :
        print "Update TNO Configuration Failure."
